import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import IndexView from '@/views/IndexView.vue'
import SearchView from '@/views/SearchView.vue'
import RepoView from '@/views/RepoView.vue'
import ImageDetail from '@/views/ImageDetailView.vue'
// import HomeView from '@/views/HomeView.vue'
// import UserView from '@/views/user/UserView.vue'
Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: '/login'
  // },
  {
    path: '/',
    name: 'index',
    component: IndexView
  },
  {
    path: '/search/:name(.*)+',
    name: 'search',
    component: SearchView
  },
  {
    path: '/repo/:name(.*)+',
    name: 'repo',
    component: RepoView
  },
  {
    path: '/image/:name(.*)+',
    name: 'image',
    component: ImageDetail
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})
// 挂载全局导航守卫
// router.beforeEach((to, from, next) => {
//   if (to === from) {
//     next()
//     return
//   }
// from从哪里来，to去哪里，next函数跳转
// 只要不是登录页，都要查看token
//   if (to.path === '/login') {
//     next()
//   } else {
//     // 读取token
//     const token = window.localStorage.getItem('token')
//     if (!token) {
//       next('/login')
//     } else {
//       next()
//     }
//   }
// })
export default router
